package server

import (
	"context"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/common/sdk/tx"
	"longmen/server/pkg/db/mysql/models/models_live"
	"longmen/server/pkg/db/mysql/models/models_main"
	"longmen/server/pkg/db/redis/cache"
	"longmen/server/pkg/protobuf"
)

func (s *Server) LiveList6T(ctx context.Context, req *protobuf.LiveList6TReq) (*protobuf.LiveList6TResp, error) {
	var err error
	var ret []*protobuf.LiveList6T
	var lives []models_live.Live
	// var include = []string{"263754", "266384", "283825", "283788", "283793", "283810", "261527", "315841", "277914", "369412", "356921", "388858"}
	size, offset := util.Pagiation(int(req.Page), int(req.Size))
	query := global.Live().Model(&lives).Where("reply_video=1").Order("hot desc").Offset(offset).Limit(size)
	if req.SportId == 1 {
		query.Where("sport_type='football'")
	}
	if req.SportId == 2 {
		query.Where("sport_type='basket'")
	}
	if req.IsReplay == 0 {
		query.Where("reply_video=0")
	}
	if req.IsReplay == 1 {
		query.Where("reply_video=1")
	}

	err = query.Scan(&lives).Error
	if err != nil {
		return nil, err
	}

	for _, v := range lives {
		leagueName := ""
		homeTeam := ""
		awayTeam := ""
		if v.Matchid > 0 {
			match := cache.MatchData(v.Matchid)
			leagueName = match.CompShortZh
			homeTeam = match.Home
			awayTeam = match.Away
		}
		ret = append(ret, &protobuf.LiveList6T{
			Id:           v.Liveid,
			Title:        v.Title,
			Anchor:       v.AnchorName,
			AnchorAvatar: v.AnchorAvatar,
			LiveThumb:    v.Thumb,
			Hot:          int32(v.Hot),
			SportType:    v.SportType,
			IsReplay:     v.ReplyVideo,
			LeagueName:   leagueName,
			HomeTeam:     homeTeam,
			AwayTeam:     awayTeam,
		})
	}

	return &protobuf.LiveList6TResp{
		List: ret,
	}, err

}

func (s *Server) LiveDetail6T(ctx context.Context, req *protobuf.LiveDetail6TReq) (*protobuf.LiveDetail6TResp, error) {
	var err error
	var live models_live.Live
	err = global.Live().Model(&live).Where("liveid=?", req.Id).Find(&live).Error
	if err != nil {
		return nil, err
	}

	leagueName := ""
	homeTeam := ""
	awayTeam := ""
	if live.Matchid > 0 {
		match := cache.MatchData(live.Matchid)
		leagueName = match.CompShortZh
		homeTeam = match.Home
		awayTeam = match.Away
	}

	var (
		m3u8 string
		flv  string
		rtmp string
	)

	if live.ReplyVideo == 1 {
		m3u8 = live.PullUrl
		flv = live.PullUrl
		rtmp = live.PullUrl
	} else {
		m3u8 = tx.CreateLiveUrl(tx.HostHLS, live.Stream, 0)[0]
		flv = tx.CreateLiveUrl(tx.HostFLV, live.Stream, 0)[0]
		rtmp = tx.CreateLiveUrl(tx.HostRTMP, live.Stream, 0)[0]
	}

	return &protobuf.LiveDetail6TResp{
		Id:             live.Liveid,
		Title:          live.Title,
		Anchor:         live.AnchorName,
		AnchorAvatar:   live.AnchorAvatar,
		LiveThumb:      live.Thumb,
		Hot:            int32(live.Hot),
		LiveStreamM3U8: m3u8,
		LiveStreamFlv:  flv,
		LiveStreamRtmp: rtmp,
		SportType:      live.SportType,
		IsReplay:       live.ReplyVideo,
		LeagueName:     leagueName,
		HomeTeam:       homeTeam,
		AwayTeam:       awayTeam,
	}, nil
}

func (s *Server) AllAnchor6T(ctx context.Context, req *protobuf.Empty) (*protobuf.AllAnchor6TResp, error) {
	var err error
	var total int64
	var users []*models_main.User
	err = global.GetX().Model(&users).Where("is_anchor=1 and status=0").Count(&total).Scan(&users).Error
	if err != nil {
		return nil, err
	}

	var ret []*protobuf.Anchor6T
	for _, v := range users {
		ret = append(ret, &protobuf.Anchor6T{
			Id:           int64(v.Id),
			Anchor:       v.NickName,
			AnchorAvatar: v.Avatar,
		})
	}

	return &protobuf.AllAnchor6TResp{
		Total: total,
		List:  ret,
	}, nil
}
